package com.xingdata.jjxc.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.xingdata.jjxc.R;
import com.xingdata.jjxc.enty.CarBrandEntity;
import com.xingdata.jjxc.views.MyImageView;

/**
 * 汽车品牌列表适配器
 * 
 * @author Administrator
 * 
 */
public class CarBrandListViewAdapter extends BaseExpandableListAdapter {
	private Context context;
	private List<CarBrandEntity> brandEntities;

	public CarBrandListViewAdapter(Context context,
			List<CarBrandEntity> brandEntities) {
		this.context = context;
		this.brandEntities = brandEntities;
	}

	@Override
	public int getGroupCount() {
		return brandEntities == null ? 0 : brandEntities.size();
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return brandEntities.get(groupPosition).getSub_branch() == null ? 0
				: brandEntities.get(groupPosition).getSub_branch().size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return brandEntities.get(groupPosition);
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return brandEntities.get(groupPosition).getSub_branch()
				.get(childPosition);
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	/**
	 * 显示：group
	 */
	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(
					R.layout.carbrand_listview_item, null);
			holder = new ViewHolder();
			holder.textView = (TextView) convertView
					.findViewById(R.id.brandName);
			holder.mgroupimage = (MyImageView) convertView
					.findViewById(R.id.mGroupimage);
//			holder.mgroupimage
//					.setBackgroundResource(R.drawable.blue_btn_selector);
			convertView.setTag(holder);

		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		if (brandEntities.get(groupPosition).getSub_branch().size() == 0) {
			holder.mgroupimage.setVisibility(View.INVISIBLE);
		} else {
			holder.mgroupimage.setVisibility(View.VISIBLE);
		}
		if (isExpanded) {
			holder.mgroupimage.setImageResource(R.drawable.listview_bg_up);
		} else {
			holder.mgroupimage.setImageResource(R.drawable.listview_bg_down);
		}
		holder.textView.setText(brandEntities.get(groupPosition)
				.getCar_branch_name());
		holder.textView.setTextSize(20);
		holder.textView.setPadding(36, 10, 0, 10);
		return convertView;

	}

	/**
	 * 显示：child
	 */
	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(
					R.layout.car_model_listview_item, null);
			holder = new ViewHolder();
			holder.textView = (TextView) convertView
					.findViewById(R.id.modle_name);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.textView.setText(brandEntities.get(groupPosition)
				.getSub_branch().get(childPosition).getCar_branch_name());
		holder.textView.setTextSize(18);
		holder.textView.setPadding(108, 10, 0, 10);
	
		return convertView;
	}

	class ViewHolder {
		TextView textView;
		MyImageView mgroupimage;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

}
